// components/index/filter/index.js
Component({
  /**
   * 组件的属性列表
   */
  properties: {
    params:{
      type: Object
    }
  },


  /**
   * 组件的初始数据
   */
  data: {
    typeRadioBox:[
      {
        value: 1,
        name: '摄影师',
        checked: false,
      }, {
        value: 2,
        name: '模特',
        checked: false,
      }
    ],
    sexRadioBox:[
      {
        value: 1,
        name: '男',
        checked: false,
      }, {
        value: 0,
        name: '女',
        checked: false,
      }
    ],
    modelCheckBox:[
      {
        value: 0,
        name: '互免约拍',
        checked: false,
      }, {
        value: 1,
        name: '需要收费',
        checked: false,
      }, {
        value: 2,
        name: '愿意付费',
        checked: false,
      }, {
        value: 3,
        name: '协商费用',
        checked: false,
      }
    ]
  },

  lifetimes: {
    attached: function() {
      this.init()
    },
    detached: function() {
      // 在组件实例被从页面节点树移除时执行
    },
  },

  /**
   * 组件的方法列表
   */
  methods: {
    reset:function () {
      let typeRadioBox = this.data.typeRadioBox;
      let sexRadioBox = this.data.sexRadioBox;
      let modelCheckBox = this.data.modelCheckBox;

      typeRadioBox.forEach(function (item,index) {
          item.checked = false
      })

      sexRadioBox.forEach(function (item,index) {
          item.checked = false
      })

      modelCheckBox.forEach(function (item,index) {
        item.checked = false
      })

      this.setData({
          typeRadioBox: typeRadioBox,
          sexRadioBox: sexRadioBox,
          modelCheckBox: modelCheckBox,
      })
      this._setReturnData()
    },
    submit:function () {
      this._setReturnData()
    },
    _setReturnData() {
      let params = this.data.params
      params.gender = this._getSingleElectionValue(this.data.sexRadioBox)
      params.capacity = this._getSingleElectionValue(this.data.typeRadioBox)
      params.chargingModel = this._getMultipleElectionValue(this.data.modelCheckBox)
      this.triggerEvent('submit', {
        params
      }, {})
    },
    radioBindHandler(data,e) {
      let items = data;
      let values = e.currentTarget.dataset.value;
      for (let i = 0, lenI = items.length; i < lenI; ++i) {
        if (items[i].value == values) {
          items[i].checked = !items[i].checked;
        }else {
          items[i].checked = false;
        }
      }
      return items;
    },

    chooseTypeBox(e) {
      this.setData({
        typeRadioBox: this.radioBindHandler(this.data.typeRadioBox,e)
      })
    },
    chooseSexBox(e) {
      this.setData({
        sexRadioBox: this.radioBindHandler(this.data.sexRadioBox,e)
      })
    },
    chooseModelCheckbox(e) {
      let items = this.data.modelCheckBox;
      let values = e.currentTarget.dataset.value;
      for (let i = 0, lenI = items.length; i < lenI; ++i) {
        if (items[i].value == values) {
          items[i].checked = !items[i].checked;
          break
        }
      }
      this.setData({
        modelCheckBox: items
      })
    },
    bindParentTap(e){
        this.triggerEvent('parentTap', {}, {})
    },
    init() {
      let typeRadioBox = this.data.typeRadioBox;
      let sexRadioBox = this.data.sexRadioBox;
      let modelCheckBox = this.data.modelCheckBox;

      let params = this.data.params
      typeRadioBox.forEach(function (item,index) {
        item.value === params.capacity?item.checked = true:item.checked = false
      })

      sexRadioBox.forEach(function (item,index) {
        item.value === params.gender?item.checked = true:item.checked = false
      })

      modelCheckBox.forEach(function (item,index) {
        params.chargingModel.forEach((item2,index2)=>{
          if (item.value == item2) {
            item.checked = true
          }
        })
      })

      this.setData({
        typeRadioBox: typeRadioBox,
        sexRadioBox: sexRadioBox,
        modelCheckBox: modelCheckBox,
      })
    },
    _getSingleElectionValue(array) {
      for (let i = 0; i < array.length; i++) {
        if (array[i].checked) {
          return array[i].value
        }
      }
      return null
    },
    _getMultipleElectionValue(array) {
      const model = []
      for (let i = 0; i < array.length; i++) {
        if (array[i].checked) {
          model.push(array[i].value)
        }
      }
      return model
    },
  }
})
